<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    public function index()
    {

        //已发送消息的标题内容时间
        $read = DB::view('message', ['session', 'title', 'id','time'])
            ->order('time','desc')
            ->limit('0,4')
            ->select();

        //查询未读消息条数
        $unread = DB('mes_bus')
            ->where([
                'status'=>'2'
            ])
            ->count('sid');



        //已发消息数量
        $summes = DB('mes_bus')
            ->count('sid');

        foreach($read as $k=>$v){
            $title[]=$v['title'];
            $time[]=$v['time'];
            $mes[]=$v['session'];
            $id[$k]=$v['id'];

            //查询当前消息总条数
            $this_read[] = DB('mes_bus')
                ->where([
                    'sid'=>$id[$k]
                ])
                ->count('sid');

            //查询当前消息已读情况
            $this_onread[] = DB('mes_bus')
                    ->where([
                        'status'=>'1',
                        'sid'=>$id[$k]
                    ])
                ->count('sid');
        }
//        session('data',$res[0]);
//        session('usernode',$node['unode']);
        session('summes',$summes);
        session('unread',$unread);
//        session('res',$read);
        session('time',$time);
        session('title',$title);
        session('mes',$mes);
        session('this_read',$this_read);
        session('this_onread',$this_onread);



        //获取信息商户总数
        $cbus = DB('business')
            ->count('id');

//        dump($cbus);

        //获取用户名
        $name = DB('business')
            ->field('name,ptime,btime')
            ->limit(0,7)
            ->select();
//        dump($name);
//        die;

        for($i=1;$i<6;$i++) {
            $times[$i] = DB('business')
                ->field('*')
                ->where('btime+ptime*86400 - unix_timestamp(now())>0 and btime+ptime*86400 - unix_timestamp(now())<86400*'.$i)
                ->count('id');
        }

        //禁用商户数
        $obus = DB('business')
            ->where('status=0')
            ->count('id');

        //过期商户数
        $gbus = DB('business')
                ->where('btime+ptime*86400 - unix_timestamp(now())<0')
                ->count('id');


//        dump($gbus);
//        dump($times);
//        dump($obus);
////
//        die;


        //价钱
        $sum = DB('money')
            ->sum('price');


        $prices = DB('money')
            ->field('price')
            ->select();

//        dump($obus);
//        die;


        if(session('data.name')){
            return view('index',[
                'cbus'=>$cbus,
                'obus'=>$obus,
                'sum'=>$sum,
                'name'=>$name,
                'times'=>$times,
                'prices'=>$prices,
                'gbus'=>$gbus,
            ]);
        }else{
            return view('login');
        }

    }

    public function login()
    {
        return view();
    }

    public function logout()
    {
        session('data.name',null);
        return View('login');
    }

    //执行登陆
    public function dologin()
    {

//        //助手函数
        $post = input('post.');
//        dump($post);
//
        $res = Db('qq_user')
            ->where( [
                'username' => $post['username'],
                'userpass' => md5($post['password'])
            ])
            ->select();
//        dump(md5($post['password']));
//        dump($res);
        if(!empty($res)) {

            //判断用户状态是否禁用
            if($res[0]['status'] == 1){
                //成功
                $result = Db::view(['qq_node' => 'ln'], ['id' => 'nodeid', 'mname', 'aname', 'status' => 'nodestatus'])
                    ->view(['qq_role_node' => 'lrn'], ['nid'], 'ln.id=lrn.nid')
                    ->view(['qq_role' => 'lr'], ['id'], 'lrn.rid = lr.id')
                    ->view(['qq_user_role' => 'lur'], ['rid'], 'lr.id = lur.rid')
                    ->view(['qq_user' => 'lu'], ['id'], 'lur.uid = lu.id')
                    ->where('lu.id', $res[0]['id'])
                    ->select();
//                 dump($result);

                //商户数
                //遍历数组存缓存
                foreach( $result as $k => $v){
                    $node['unode'][] = strtolower($result[$k]['mname'].' '.strtolower($result[$k]['aname']));
                    $node['mname'][]= $result[$k]['mname'];
                    $node['aname'][]= $result[$k]['aname'];
                }


                //已发送消息的标题内容时间
                $read = DB::view('message', ['session', 'title', 'id','time'])
                    ->order('time','desc')
                    ->limit('0,4')
                    ->select();

                //查询未读消息条数
                $unread = DB('mes_bus')
                    ->where([
                        'status'=>'2'
                    ])
                    ->count('sid');



                //已发消息数量
                $summes = DB('mes_bus')
                    ->count('sid');


                foreach($read as $k=>$v){
                    $title[]=$v['title'];
                    $time[]=$v['time'];
                    $mes[]=$v['session'];
                    $id[$k]=$v['id'];

                    //查询当前消息总条数
                    $this_read[] = DB('mes_bus')
                        ->where([
                            'sid'=>$id[$k]
                        ])
                        ->count('sid');

                    //查询当前消息已读情况
                    $this_onread[] = DB('mes_bus')
                        ->where([
                            'status'=>'1',
                            'sid'=>$id[$k]
                        ])
                        ->count('sid');
                }


                session('data',$res[0]);
                session('usernode',$node['unode']);
                session('summes',$summes);
                session('unread',$unread);
                session('res',$read);
                session('time',$time);
                session('title',$title);
                session('mes',$mes);
                session('this_read',$this_read);
                session('this_onread',$this_onread);


//                dump(session('name'));

//                die;
//                 dump(session('usernode'));
//                die;
                //成功跳转
                $this->success('登录成功','index/index/index',null,3);
            }else{
                $this->error('你的输入有误');
            }
            $this->error('登录失败');
        }
        $this->error('你的登录失败');
    }
}
